package com.hxd.mall.utils.open

import android.Manifest
import android.app.Activity
import androidx.fragment.app.FragmentActivity
import com.hexin.locallibrary.permssion.PermissionGrantedCallback
import com.hexin.locallibrary.permssion.PermissionUtils
import com.hexin.locallibrary.permssion.PermissionUtils.showPermissions
import com.hxd.mall.R
import com.hxd.mall.constant.HxdGlobalInfo
import android.content.Intent
import android.content.Context
import android.net.Uri
import android.provider.Settings
import android.provider.Settings.*


object PermissionUtil {
    /**
     * 判断是否有权限集合
     */
    fun isPermissions(permissions: Array<String>): Boolean {
        for (permission in permissions) {
            if (isPermission(permission)) {
                return false
            }
        }
        return true
    }

    /**
     * 判断是否有权限
     */
    fun isPermission(permission: String) = PermissionUtils.isOpenPermisson(HxdGlobalInfo.instance, permission)

    /**
     * 主app权限申请
     */
    fun grantPermission(activity: FragmentActivity, isAutoTip: Boolean, permissionGrantedCallback: PermissionGrantedCallback?) {
        if (activity.isFinishing || activity.isDestroyed) {
            return
        }
//        RxPermissions(activity).request(
//                Manifest.permission.WRITE_EXTERNAL_STORAGE,
//                Manifest.permission.READ_EXTERNAL_STORAGE,
//                android.Manifest.permission.READ_PHONE_STATE,
//                android.Manifest.permission.ACCESS_COARSE_LOCATION,
//                android.Manifest.permission.ACCESS_FINE_LOCATION,
//                android.Manifest.permission.READ_CALL_LOG,
//                android.Manifest.permission.READ_CONTACTS,
//                android.Manifest.permission.READ_SMS,
//                android.Manifest.permission.ACCESS_WIFI_STATE,
//                android.Manifest.permission.ACCESS_NETWORK_STATE,
//                android.Manifest.permission.CHANGE_WIFI_STATE
//        ).subscribe {
//            callback(it)
//        }

        //强制性让打开权限
        PermissionUtils.permissionCheck(
            activity, arrayOf(
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                android.Manifest.permission.READ_PHONE_STATE,
                android.Manifest.permission.ACCESS_COARSE_LOCATION,
                android.Manifest.permission.ACCESS_FINE_LOCATION,
                android.Manifest.permission.READ_CALL_LOG,
                android.Manifest.permission.READ_CONTACTS,
                android.Manifest.permission.READ_SMS,
                android.Manifest.permission.ACCESS_WIFI_STATE,
                android.Manifest.permission.ACCESS_NETWORK_STATE,
                android.Manifest.permission.CHANGE_WIFI_STATE
            ), tipPermissionText(), isAutoTip, permissionGrantedCallback
        )
    }

    fun tipPermissionText() = "存储、电话、位置信息、相机、通讯录、信息、通话记录等"

    /**
     * 未授权提示
     */
    fun tipPermission(context: Activity) {
        var msg = ""
        if (!isPermission(Manifest.permission.READ_SMS)) {
            msg = context.getString(R.string.permission_tip_sms)
        } else if (!isPermission(Manifest.permission.READ_CALL_LOG)) {
            msg = context.getString(R.string.permission_tip_call)
        } else if (!isPermission(Manifest.permission.READ_CONTACTS)) {
            msg = context.getString(R.string.permission_tip_contact)
        } else if (!isPermission(Manifest.permission.ACCESS_COARSE_LOCATION)
            || !isPermission(Manifest.permission.ACCESS_FINE_LOCATION)
        ) {
            msg = context.getString(R.string.permission_tip_location)
        } else if (!isPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) ||
            !isPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
        ) {
            msg = context.getString(R.string.permission_tip_sd_card)
        } else if (!isPermission(Manifest.permission.READ_PHONE_STATE)) {
            msg = context.getString(R.string.permission_tip_phone_statue)
        } else {
            msg = context.getString(R.string.permission_tip)
        }
        showPermissions(context, msg, 10009) {}
    }

    fun toOpenNotify(context: Context) {
        try {
            // 根据isOpened结果，判断是否需要提醒用户跳转AppInfo页面，去打开App通知权限
            val intent = Intent()
            intent.action = ACTION_APP_NOTIFICATION_SETTINGS
            //这种方案适用于 API 26, 即8.0（含8.0）以上可以用
            intent.putExtra(EXTRA_APP_PACKAGE, context.packageName)
            intent.putExtra(EXTRA_CHANNEL_ID, context.applicationInfo.uid)

            //这种方案适用于 API21——25，即 5.0——7.1 之间的版本可以使用
            intent.putExtra("app_package", context.packageName)
            intent.putExtra("app_uid", context.applicationInfo.uid)

            // 小米6 -MIUI9.6-8.0.0系统，是个特例，通知设置界面只能控制"允许使用通知圆点"——然而这个玩意并没有卵用，我想对雷布斯说：I'm not ok!!!
            //  if ("MI 6".equals(Build.MODEL)) {
            //      intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            //      Uri uri = Uri.fromParts("package", getPackageName(), null);
            //      intent.setData(uri);
            //      // intent.setAction("com.android.settings/.SubSettings");
            //  }
            context.startActivity(intent)
        } catch (e: Exception) {
            e.printStackTrace()
            // 出现异常则跳转到应用设置界面：锤子坚果3——OC105 API25
            val intent = Intent()

            //下面这种方案是直接跳转到当前应用的设置界面。
            //https://blog.csdn.net/ysy950803/article/details/71910806
            intent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
            val uri = Uri.fromParts("package", context.packageName, null)
            intent.data = uri
            context.startActivity(intent)
        }
    }

}